package com.flyme.module.file.provider.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.flyme.common.core.constants.CommonConstants;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.event.SliserAddEvent;
import com.flyme.module.file.client.constants.FileConstants;
import com.flyme.module.file.client.entity.SysCourse;
import com.flyme.module.file.provider.mapper.SysCourseMapper;
import com.flyme.module.file.provider.service.SysCourseService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author: flyme
 * @date: 2018/3/7 15:27
 * @desc: 课程管理接口实现
 */
@Service
public class SysCourseServiceImpl extends BaseServiceImpl<SysCourseMapper, SysCourse> implements SysCourseService {



    @Override
    public Boolean addCourse(SysCourse course) {
        course.setCourseStatus(FileConstants.COURSE_PUB);
        return save(course);
    }


    @Override
    public Boolean updateCourse(SysCourse course) {
        if(course.getIsSlider().equals(CommonConstants.ENABLED)) {
            applicationContext.publishEvent(new SliserAddEvent(this, SysCourse.class.getSimpleName(), course.getId(), course.getCourseTitle(), course.getCourseImage()));
        }
        return saveOrUpdate(course);
    }

    @Override
    public ResultBody delByIds(String[] ids) {
        remove(q().in("id",ids));
        return ResultBody.success("刪除成功");
    }

    /**
     * 网站首页查询课程
     * @return
     */
    public List<EntityMap> selectInderCourse(PagerInfo pagerInfo, String indextagId) {
        pagerInfo.setPageSize(10);
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("c.id,courseTitle,courseImage,recordNum,indextagId,lecturerId,(select count(ca.id) from sys_catalog ca where ca.courseId=c.id and parentId!=0)courseCount," +
            "coursePrice,isFree,isVipFree,iosPrice,user.avatar,user.userName");
        cq.eq("recommend", CommonConstants.ENABLED);
        if(ObjectUtils.isNotEmpty(indextagId)){
            cq.in("indextagId", indextagId);
        }
        cq.eq("isContinue", 0);
        cq.orderByDesc("recordNum");
        cq.createAlias("user", "user");
        List<EntityMap> list = (List<EntityMap>) pageList(cq).getRows();
        return list;
    }


    /**
     * 搜索课程
     */
    public List<EntityMap> searchCourse(BigDecimal iosRatio,String keyWord, PagerInfo pagerInfo) {
        pagerInfo.setPageSize(10);
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.addSql("c.id,c.courseTitle,c.recordNum,c.courseImage,c.isFree,c.isVipFree,c.coursePrice,lecturer.lecturerName,c.onLine");
        queryWrapper.addSql("(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount");
        queryWrapper.addSql("(round(c.coursePrice*"+iosRatio+")) iosPrice");
        queryWrapper.like("courseTitle", keyWord);
        queryWrapper.eq("courseStatus", 1);
        queryWrapper.eq("onLine", 1);
        queryWrapper.eq("isContinue", 0);
        queryWrapper.orderByAsc("courseOrder");
        queryWrapper.createAlias("lecturer", "lecturer");
        List<EntityMap> list = (List<EntityMap>) pageList(queryWrapper).getRows();
        return list;
    }
    /**
     * 搜索课程
     */
    public PqGrid searchpageCourse(BigDecimal iosRatio,String keyWord, PagerInfo pagerInfo) {
        pagerInfo.setPageSize(10);
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.addSql("c.id,c.courseTitle,c.recordNum,c.courseImage,c.isFree,c.isVipFree,c.coursePrice,lecturer.lecturerName,c.onLine");
        queryWrapper.addSql("(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount");
        queryWrapper.addSql("(round(c.coursePrice*"+iosRatio+")) iosPrice");
        queryWrapper.like("courseTitle", keyWord);
        queryWrapper.eq("courseStatus", 1);
        queryWrapper.eq("onLine", 0);//线下课程
        queryWrapper.eq("isContinue", 0);
        queryWrapper.orderByAsc("courseOrder");
        queryWrapper.createAlias("lecturer", "lecturer");
        return  pageList(queryWrapper);
    }


    /**
     * PC端搜索课程
     */
    public PqGrid searchCourseWeb(BigDecimal iosRatio, String keyWord, PagerInfo pagerInfo) {
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.addSql("c.id,c.courseTitle,c.courseImage,c.isFree,c.isVipFree,c.coursePrice,lecturer.lecturerName");
        queryWrapper.addSql("(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount");
        queryWrapper.addSql("(round(c.coursePrice*"+iosRatio+")) iosPrice");
        queryWrapper.like("courseTitle", keyWord);
//		queryWrapper.or();
//		queryWrapper.like("lecturerName", keyWord);
        queryWrapper.eq("courseStatus", 1);
        queryWrapper.eq("isContinue", 0);
        queryWrapper.orderByAsc("courseOrder");
        queryWrapper.createAlias("lecturer", "l");
        return pageList(queryWrapper);
    }

    /**
     * 根据讲师查询对应的所有课程
     */
    public List<EntityMap> selectByLecturer(BigDecimal iosRatio,String lecturerId, PagerInfo pagerInfo) {
        pagerInfo.setPageSize(10);
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.addSql("c.courseImage,c.recordNum,c.courseTitle,c.courseBeginDate,c.coursePrice,c.id courseId,lecturer.lecturerName,lecturer.lecturerImg,c.onLine,c.isFree,c.isVipFree");
        queryWrapper.addSql("(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount");
        queryWrapper.addSql("(round(c.coursePrice*"+iosRatio+")) iosPrice");
        queryWrapper.eq("lecturerId", lecturerId);
        queryWrapper.eq("courseStatus", 1);
        queryWrapper.eq("isContinue", 0);
        queryWrapper.orderByAsc("courseOrder");
        queryWrapper.createAlias("lecturer", "lecturer");
        return (List<EntityMap>) pageList(queryWrapper).getRows();
    }

    /**
     * 网站根据讲师查询推荐课程
     */
    public List<EntityMap> selectLecturerRecommends(BigDecimal iosRatio,Long lecturerId, PagerInfo pagerInfo) {
        pagerInfo.setPageSize(100);
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.addSql("c.courseImage,c.courseTitle,c.coursePrice,c.id courseId,lecturer.lecturerName,lecturer.lecturerImg,c.isFree,c.isVipFree");
        queryWrapper.addSql("(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount");
        queryWrapper.addSql("(round(c.coursePrice*"+iosRatio+")) iosPrice");
        queryWrapper.eq("lecturerId", lecturerId);
        queryWrapper.eq("courseStatus", 1);
        queryWrapper.eq("isContinue", 0);
        queryWrapper.orderByAsc("courseOrder");
        queryWrapper.createAlias("lecturer", "l");
        return (List<EntityMap>) pageList(queryWrapper).getRows();
    }

    /**
     * 网站查询全部课程列表
     */
    public PqGrid selectCourseAllWeb(BigDecimal iosRatio,PagerInfo pagerInfo, String sort) {
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.addSql("c.id,c.courseTitle,c.courseImage,c.lecturerId,c.coursePrice,lecturer.lecturerImg,lecturer.lecturerName,c.isFree,c.isVipFree");
        queryWrapper.addSql("(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount");
        queryWrapper.addSql("(round(c.coursePrice*"+iosRatio+")) iosPrice");
        queryWrapper.eq("courseStatus", 1);
        queryWrapper.eq("isContinue", 0);
        queryWrapper.createAlias("lecturer", "l");
        setQuery(sort, queryWrapper);
        return pageList(queryWrapper);
    }

    /**
     * 网站查询推荐课程列表
     */
    public PqGrid selectCourseRecommendsWeb(BigDecimal iosRatio,PagerInfo pagerInfo, String sort) {
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.addSql("c.id,c.courseTitle,c.courseImage,c.lecturerId,c.coursePrice,lecturer.lecturerImg,lecturer.lecturerName,c.isFree,c.isVipFree");
        queryWrapper.addSql("(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount");
        queryWrapper.addSql("(round(c.coursePrice*"+iosRatio+")) iosPrice");
        queryWrapper.eq("recommend", 1);
        queryWrapper.eq("courseStatus", 1);
        queryWrapper.eq("isContinue", 0);
        queryWrapper.createAlias("lecturer", "l");
        setQuery(sort, queryWrapper);
        return pageList(queryWrapper);
    }

    private void setQuery(String sort, CriteriaQuery cq) {
        if (ObjectUtils.isNotEmpty(sort)) {//排序
            if (sort.equals("S1")) {//免费
                cq.eq("isFree", 0);
            }
            if (sort.equals("S2")) {//vip
                cq.eq("isVipFree", 0);
                cq.eq("isFree", 1);
                cq.gt("coursePrice", 0);
            }
            if (sort.equals("S3")) {//价格升序
                cq.orderByAsc("coursePrice");
            }
            if (sort.equals("S4")) {//价格降序
                cq.orderByDesc("coursePrice");
            }
        } else {
            cq.orderByAsc("courseOrder");
        }
    }

    /**
     * 网站查询一级课程列表
     */
    public PqGrid selectCourseByTypeFirstWeb(PagerInfo pagerInfo, List<Object> ids, String sort) {
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        QueryWrapper select = queryWrapper.select("c.id,c.courseTitle,c.courseImage,c.lecturerId,(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount,c.coursePrice,lecturer.lecturerImg,lecturer.lecturerName,c.isFree,c.isVipFree");
        queryWrapper.in("courseTypeId", ids);
        queryWrapper.eq("courseStatus", 1);
        queryWrapper.eq("isContinue", 0);
        queryWrapper.createAlias("lecturer", "l");
        setQuery(sort, queryWrapper);
        return pageList(queryWrapper);
    }

    /**
     * 网站查询课程列表
     */
    public PqGrid selectCourseByTypeWeb(PagerInfo pagerInfo, String coursetypeId, String sort) {
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("c.id,c.courseTitle,c.courseImage,c.lecturerId,(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount,c.coursePrice,lecturer.lecturerImg,lecturer.lecturerName,c.isFree,c.isVipFree");
        queryWrapper.eq("courseTypeId", coursetypeId);
        queryWrapper.eq("courseStatus", 1);
        queryWrapper.eq("isContinue", 0);
        queryWrapper.createAlias("lecturer", "l");
        setQuery(sort, queryWrapper);
        return pageList(queryWrapper);
    }

    /**
     * 网站首页展示的课程
     */
    public List<EntityMap> selectInderCourseWeb(Integer num) {
        PagerInfo pagerInfo = new PagerInfo();
        pagerInfo.setPageSize(num);
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("c.id,courseTitle,courseTypeId,courseImage,courseTime,courseBeginDate," +
//            "(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount,coursePrice,c.isFree,isVipFree,user.userName lecturerName,user.avatar lecturerImg");
            "(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount,coursePrice,c.isFree,isVipFree,lecturer.lecturerName,lecturer.lecturerImg");
//        queryWrapper.createAlias("user", "l");
        queryWrapper.createAlias("lecturer", "l");
        queryWrapper.eq("isContinue", 0);
        queryWrapper.eq("recommend", 1);
        List<EntityMap> list = (List<EntityMap>) pageList(queryWrapper).getRows();
        return list;
    }

    /**
     * 网站根据讲师查询对应的所有课程
     */
    public PqGrid selectByLecturerWeb(Long lecturerId, String sortOrder, PagerInfo pagerInfo) {
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("c.courseImage,c.courseTitle,CASE WHEN ISNULL(c.coursePrice) THEN 0 ELSE c.coursePrice END coursePrice," +
            "(select count(id) from sys_catalog ca where ca.courseId=c.id and parentId!=0) courseCount,c.id,lecturer.lecturerName,lecturer.lecturerImg,c.isVipFree,c.isFree,c.iosPrice");
        queryWrapper.eq("lecturerId", lecturerId);
        queryWrapper.eq("courseStatus", 1);
        queryWrapper.eq("isContinue", 0);
        queryWrapper.orderByAsc("courseOrder");
        queryWrapper.createAlias("lecturer", "l");
        setQuery(sortOrder, queryWrapper);
        return pageList(queryWrapper);
    }

    /**
     * 根据模块统计课程数量
     */
    public Integer countByTag(Long tagId){
        return count(q().eq("indextagId", tagId));
    }

}
